package com.tianqu.tms.www.transfer.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.security.TmsUserProfileFactory;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.entity.Vendor;

public class TransferLiveDetailController extends TmsController {
	private Logger logger = Logger.getLogger(TransferLiveDetailController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		TmsUserProfileFactory userfactory = TmsUserProfileFactory.getInstance();
		TmsUserProfile userprofile = userfactory.getUserProfile("root");
		User user = (User) userfactory.getTmsUser("root", "tianqu");
		uc.setUserProfile(userprofile);
		UserAuth auth = UserAuth.getInstance();
		List<UserAuth> list = auth.getAuth(user.getId());
		
		List funcId = new ArrayList();
		for (UserAuth Auth : list) {
			//根据roleId获取funcId
			UserRole role =new UserRole();
			role = role.findRoleByRole(Auth.getrId());
			if(role.getFuncIds()!=null)
			{
				String[] bb = role.getFuncIds().split(",");
				List yyList = Arrays.asList(bb);	
				for(int i=0;i<yyList.size();i++)
				{
					funcId.add(yyList.get(i));
				}
			}
		}

		// Userprofile赋值
		setUserProfile(userprofile, user, funcId);
		// 保存到UC里面
		uc.setUserProfile(userprofile);
		/*
		 * 加载字典表
		 */
		Codes.getInstance();
		// 品牌
		Vendor.getInstance();
		// 车系
		VehicleBrand.getInstance();
		// 款式
		CarStyle.getInstance();
		//大车品牌车系款式
		CommercialVehicle.getInstance();
		controller.commit();
		logger.info(" 登录成功!");
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * TmsUserProfile的设置
	 * 
	 * @param userprofile
	 * @param user
	 * @param roleimpl
	 * @param customers
	 */
	void setUserProfile(TmsUserProfile userprofile, User user, List list) {
		userprofile.setUniqueId((int) user.getId());
		userprofile.setLoginId(user.getLoginId());
		// 真实姓名
		userprofile.setRealName(user.getRealName());
		userprofile.setUserClass(Integer.parseInt(user.getUcId()));// 权限类型
		userprofile.setUserClassList(list);
		userprofile.setUserType(Integer.parseInt(user.getUserType()));
	} 
}
